package lovo.practice_7_14;

import lovo.Util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ScanPanel extends JPanel implements ActionListener {


    private int falsetime = 3;

    public int getFalsetime() {
        return falsetime;
    }

    public void setFalsetime(int falsetime) {
        this.falsetime = falsetime;
    }

    private J189_7_14 frame;
    private JTextField jTextFieldAccount;
    private JPasswordField jPasswordField;
    private JButton jButtonLogin;
    private Icon ImageLogin = Util.getimagesIcon("/Images/buttonLand.jpg");


    public ScanPanel(J189_7_14 frame) {

        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jTextFieldAccount = new JTextField();
        jTextFieldAccount.setBounds(400, 400, 150, 40);
        this.add(jTextFieldAccount);
        jPasswordField = new JPasswordField();
        jPasswordField.setBounds(400, 500, 150, 40);
        this.add(jPasswordField);

        jButtonLogin = new JButton(ImageLogin);
        jButtonLogin.setBounds(600, 500, 140, 50);
        this.add(jButtonLogin);
        jButtonLogin.addActionListener(this);



    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtonLogin) {
            for (int i = 0; i < frame.getListusers().size(); i++) {
                if (jTextFieldAccount.getText().equals(frame.getListusers().get(i).getAccount()) && jPasswordField.getText().equals(frame.getListusers().get(i).getPwd())) {
                     frame.setUsers(frame.getListusers().get(i));
                    frame.switchPanel("mune");
                    return;
                }
            }
            falsetime--;

            if (falsetime == 0) {
                JOptionPane.showMessageDialog(this, "3次机会用完，你卡没了");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "登录失败，你还有" + falsetime + "次机会");

        }
    }

}


